package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcloud.vod.VodUploadClient;
import com.qcloud.vod.model.VodUploadRequest;
import com.qcloud.vod.model.VodUploadResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import kotlin.jvm.Volatile;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("/api/album")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;
	@Autowired
	private VodService vodService;
	//

	/**
	 * /api/album/trackInfo/getTrackStatVo/{trackId}
	 * 获取声音统计信息
	 * @param trackId
	 * @return
	 */
	@GetMapping("/trackInfo/getTrackStatVo/{trackId}")
	public Result<TrackStatVo> getTrackStatVo(@PathVariable Long trackId){
		TrackStatVo trackStatVo = trackInfoService.getTrackStatVo(trackId);
		return Result.ok(trackStatVo);
	}


	/**
	 * 根据专辑ID获取专辑统计信息
	 * /api/album/albumInfo/getAlbumStatVo/{albumId}
	 * @param albumId
	 * @return
	 */
	@GetMapping("/albumInfo/getAlbumStatVo/{albumId}")
	public Result<AlbumStatVo> getAlbumStatVo(@PathVariable Long albumId){
		AlbumStatVo albumStatVo=  trackInfoService.getAlbumStatVo(albumId);
		return Result.ok(albumStatVo);
	}

	/**
	 * 查询专辑声音分页列表
	 * /api/album/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}
	 * @param albumId
	 * @param page
	 * @param limit
	 * @param userId
	 * @return
	 */
	@GuiGuLogin(require = false)
	@GetMapping("/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}")
	public Result<Page<AlbumTrackListVo>> findAlbumTrackPage(@PathVariable Long albumId, @PathVariable Long page, @PathVariable Long limit){
		Long userId = AuthContextHolder.getUserId();
		Page<AlbumTrackListVo> albumTrackListVoPage = new Page<>(page, limit);
		Page<AlbumTrackListVo> albumTrackPage = trackInfoService.findAlbumTrackPage(albumTrackListVoPage,albumId,userId);
		return Result.ok(albumTrackPage);

	}

	/**
	 * 删除声音信息
	 * //api/album/trackInfo/removeTrackInfo/{id}
	 * @param id
	 * @return
	 */

	@GuiGuLogin
	@DeleteMapping("trackInfo/removeTrackInfo/{id}")
	public Result removeTrackInfo(@PathVariable Long id){
		trackInfoService.removeTrackInfo(id);
		return Result.ok();
	}

	/**
	 * 修改声音信息
	 * /api/album/trackInfo/updateTrackInfo/{id}
	 * @param id
	 * @param trackInfoVo
	 * @return
	 */
	@GuiGuLogin
	@PutMapping("trackInfo/updateTrackInfo/{id}")
	public Result updateTrackInfo(@PathVariable Long id ,
								  @RequestBody TrackInfoVo trackInfoVo){


		trackInfoService.updateTrackInfo(id,trackInfoVo);

		return Result.ok();
	}



	/**
	 * 查询声音信息
	 * //trackInfo/getTrackInfo/{id}
	 * @param id
	 * @return
	 */
	@GuiGuLogin
	@GetMapping("trackInfo/getTrackInfo/{id}")

	public Result<TrackInfo> getTrackInfo(@PathVariable Long id){
		TrackInfo trackInfo =	trackInfoService.getById(id);
		return Result.ok(trackInfo);
	}


	/**
	 * 获取当前用户声音分页列表
	 * /api/album/trackInfo/findUserTrackPage/{page}/{limit}
	 * @param page
	 * @param limit
	 * @param query
	 * @return
	 */
	@GuiGuLogin
	@PostMapping("trackInfo/findUserTrackPage/{page}/{limit}")
	public Result<Page<TrackListVo>> findUserTrackPage(@PathVariable Long page,
												 @PathVariable Long limit,
												 @RequestBody TrackInfoQuery query){
		//获取用户ID
		Long userId = AuthContextHolder.getUserId();
		query.setUserId(userId);

		//封装分页对象
		Page<TrackListVo> trackListVoPage = new Page<>(page, limit);
		//调用service查询
		trackListVoPage=trackInfoService.findUserTrackPage(trackListVoPage,query);

		return Result.ok(trackListVoPage);

	}



	/**
	 * 保存声音
	 * /api/album/trackInfo/saveTrackInfo
	 * @param trackInfoVo
	 * @return
	 */
	@GuiGuLogin
	@PostMapping("trackInfo/saveTrackInfo")
	public Result saveTrackInfo(@RequestBody @Validated TrackInfoVo trackInfoVo){
		Long userId = AuthContextHolder.getUserId();
		trackInfoService.saveTrackInfo(userId,trackInfoVo);
		return Result.ok();

	}

	/**
	 * 上传声音
	 * ///api/album/trackInfo/uploadTrack
	 * SecretId:AKIDa2VwEJBqKadS3TgfR0OdicJo8GF0XtdI
	 * SecretKey:GLLJDuODSZVFpERSFoCQTJvMabO0PccR
	 * @param file
	 * @return
	 */
	@PostMapping("trackInfo/uploadTrack")

	public Result<Map<String,String>> uploadTrack(MultipartFile file){
		Map<String,String> stringStringMap =vodService.uploadTrack(file);
		return Result.ok(stringStringMap);


	}


}

